﻿using Furion.EventBridge;
using Mapster;

namespace Jiffy.Core.Log
{
    /// <summary>
    /// 日志订阅处理
    /// </summary>
    [EventHandler]
    public class LogEventHandler : IEventHandler
    {
        /// <summary>
        /// 访问日志仓储
        /// </summary>
        private readonly ILogRepository<SysLogVis> _logVisRepo;
        /// <summary>
        /// 操作日志仓储
        /// </summary>
        private readonly ILogRepository<SysLogOp> _logOpRepo;
        /// <summary>
        /// 审计日志仓储
        /// </summary>
        private readonly ILogRepository<SysLogAudit> _logAuditRepo;
        /// <summary>
        /// 异常日志仓储
        /// </summary>
        private readonly ILogRepository<SysLogEx> _logExRepo;

        public LogEventHandler(
            ILogRepository<SysLogVis> logVisRepo,
            ILogRepository<SysLogOp> logOpRepo,
            ILogRepository<SysLogAudit> logAuditRepo,
            ILogRepository<SysLogEx> logExRepo)
        {
            _logVisRepo = logVisRepo;
            _logOpRepo = logOpRepo;
            _logAuditRepo = logAuditRepo;
            _logExRepo = logExRepo;
        }


        /// <summary>
        /// 记录访问日志
        /// </summary>        
        [EventMessage]
        public async void Visited(EventMessage eventMessage)
        {
            SysLogVis log = eventMessage.Payload.Adapt<SysLogVis>();
            await _logVisRepo.InsertNowAsync(log);
        }

        /// <summary>
        /// 记录操作日志
        /// </summary>                
        [EventMessage]
        public async void Op(EventMessage eventMessage)
        {
            SysLogOp log = eventMessage.Payload.Adapt<SysLogOp>();
            await _logOpRepo.InsertNowAsync(log);
        }


        /// <summary>
        /// 记录审记日志
        /// </summary>
        [EventMessage]
        public async void Audit(EventMessage eventMessage)
        {
            SysLogAudit log = eventMessage.Payload.Adapt<SysLogAudit>();
            await _logAuditRepo.InsertNowAsync(log);
        }

        /// <summary>
        /// 记录异常日志
        /// </summary>
        [EventMessage]
        public async void Ex(EventMessage eventMessage)
        {
            SysLogEx log = eventMessage.Payload.Adapt<SysLogEx>();
            await _logExRepo.InsertNowAsync(log);
        }
    }
}